<?php

/**
 * @Author:      lyt
 * @DateTime:    2015-10-08 11:44:44
 * @Description: 用于维护耗材 的维护和管理
 *
 */

class SupplyItemNonStandard extends Eloquent
{

    protected $table = "supply_item_non_standard";



    public static $status = array(
        "1"=>"开启",
        "0"=>"关闭",
    );

    public function subjectobj()
    {
        return $this->hasOne('SupplySubject',"id","subject");
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-04-19 10:46:39
     * @Description: 根据门店 和车检索条件 车条件类型 检索条件获取 该门店的 符合条件的耗材id
     */
    public static  function retrievalCode($shop_id,$car_retrieval_code,$type,$retrieval_code,$car_str="",$level_type=0,$level_code=0){
    	$sins=SupplyItemNonStandard::where("supply_item_non_standard.shop_id",$shop_id);
    	// ->orderBy("supply_item_non_standard.created_at","desc");
        if($car_retrieval_code&&$type){//如果存在 检索条件和类型
            $sins=$sins->leftJoin("supply_product_non_item","supply_product_non_item.supply_non_item_id","=","supply_item_non_standard.id")
                ->leftJoin("anhui_data.car_standard_info","anhui_data.car_standard_info.id","=","supply_product_non_item.car_model_id")
            ->where("supply_product_non_item.status",1);
            switch ($type) {
                case 1://如果是品牌
                    $sins=$sins->where("car_standard_info.brand_name_id",$car_retrieval_code);
                    break;
                case 2://如果是系列
                    $sins=$sins->where("car_standard_info.series_name_id",$car_retrieval_code);
                    break;
                case 3://如果是年款
                    $c_arr=explode(',', $car_retrieval_code);
                    if(isset($c_arr[0])){
                        $sins=$sins->where("car_standard_info.series_name_id",$c_arr[0]);
                        if(isset($c_arr[1])){
                            $sins=$sins->where("car_standard_info.model_year",$c_arr[1]);
                        }
                    }
                    break;
                case 4://如果是型号
                    $sins=$sins->where("car_standard_info.id",$car_retrieval_code);
                    break;
            }
        }
        if($level_type!=0&&$level_code!=0){//如果存在 三级分类检索天剑
            $sins=$sins->leftJoIn("statistical_classification as sc3","sc3.id","=","supply_item_non_standard.st_classifi_id")
            ->leftJoIn("statistical_classification as sc2","sc2.id","=","sc3.parent_id")
            ->leftJoIn("statistical_classification as sc1","sc1.id","=","sc2.parent_id");
            switch ($level_type) {
                case 1://如果是一级
                    $sins=$sins->where("sc1.id",$level_code);
                    break;
                case 2://如果二级
                    $sins=$sins->where("sc2.id",$level_code);
                    break;
                case 3://如果三级
                    $sins=$sins->where("sc3.id",$level_code);
                    break;
            }
        }
    	// if($car_model_id){
    	// 	$sins=$sins->leftJoin("supply_product_non_item","supply_product_non_item.supply_non_item_id","=","supply_item_non_standard.id")->where("supply_product_non_item.car_model_id",$car_model_id);
    	// }
    	if($retrieval_code){
            // $result=Scws::participle($retrieval_code);
            // Log::info($result);
            // if($result["code"]==0){//说明分词成功    
            //     foreach ($result["result"] as $re) {
            //         $sins=$sins->where("supply_item_non_standard.retrieval_code","like","%".$re."%");
            //     }
            // }else{//说明分词失败
            //     $sins=$sins->where("supply_item_non_standard.retrieval_code","like","%".$retrieval_code."%");
            // }    
    		$sins=$sins->where("supply_item_non_standard.retrieval_code","like","%".$retrieval_code."%");
    	}
        if($car_str){
            $sins=$sins->where("supply_item_non_standard.car_model","like","%".$car_str."%");                        
        }
    	$sind=$sins->select("supply_item_non_standard.id")->get();
    	$temp_id=array();
    	foreach ($sind as $_s) {
    		$temp_id[]=$_s->id;
    	}
    	return array_unique($temp_id);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-11 14:33:26
     * @Description: 检索条件获取 该门店的 符合条件的耗材id
     */
    public static  function retrievalCodeNew($shop_id,$retrieval_code,$car_str="",$level_type=0,$level_code=0,$three){
        $sins=SupplyItemNonStandard::where("supply_item_non_standard.shop_id",$shop_id);
        if($level_type!=0&&$level_code!=0){//如果存在 三级分类检索天剑
            $sins=$sins->leftJoIn("statistical_classification as sc3","sc3.id","=","supply_item_non_standard.st_classifi_id")
            ->leftJoIn("statistical_classification as sc2","sc2.id","=","sc3.parent_id")
            ->leftJoIn("statistical_classification as sc1","sc1.id","=","sc2.parent_id");
            switch ($level_type) {
                case 1://如果是一级
                    $sins=$sins->where("sc1.id",$level_code);
                    break;
                case 2://如果二级
                    $sins=$sins->where("sc2.id",$level_code);
                    break;
                case 3://如果三级
                    $sins=$sins->where("sc3.id",$level_code);
                    break;
            }
        }
        if($retrieval_code){
            // $result=Scws::participle($retrieval_code);
            // Log::info($result);
            // if($result["code"]==0){//说明分词成功    
            //     foreach ($result["result"] as $re) {
            //         $sins=$sins->where("supply_item_non_standard.retrieval_code","like","%".$re."%");
            //     }
            // }else{//说明分词失败
            //     $sins=$sins->where("supply_item_non_standard.retrieval_code","like","%".$retrieval_code."%");
            // } 
            $sins=$sins->where("supply_item_non_standard.retrieval_code","like","%".$retrieval_code."%");
        }
        if($car_str){
            $sins=$sins->where("supply_item_non_standard.car_model","like","%".$car_str."%");                        
        }
        if(sizeof($three)>0){
            $sins=$sins->whereIn('st_classifi_id',$three);
        }
        $sind=$sins->select("supply_item_non_standard.id")->get();
        $temp_id=array();
        foreach ($sind as $_s) {
            $temp_id[]=$_s->id;
        }
        return array_unique($temp_id);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-04-22 11:32:29
     * @Description: 根据id获取 非标准耗材的信息
     */
    public static function getCacheItemInfo($item_id){
        if(Cache::has('supply_non_item_'.$item_id)){//如果有缓存的 非标准耗材
            return Cache::get('supply_non_item_'.$item_id);
        }else{
            $si_arr=SupplyItemNonStandard::
            leftJoin("statistical_classification","statistical_classification.id","=","supply_item_non_standard.st_classifi_id")->
            leftJoin("supply_subject","supply_subject.id","=","supply_item_non_standard.subject")->
            select("supply_item_non_standard.id","supply_item_non_standard.shop_id","supply_item_non_standard.car_model","supply_item_non_standard.code","supply_item_non_standard.name","supply_item_non_standard.subject","supply_subject.name as subject_name","supply_item_non_standard.retrieval_code","supply_item_non_standard.brand_name","supply_item_non_standard.series_name","supply_item_non_standard.model","supply_item_non_standard.unit","supply_item_non_standard.bz_unit","supply_item_non_standard.pack_unit as pack","supply_item_non_standard.status","supply_item_non_standard.amount","supply_item_non_standard.specification","supply_item_non_standard.old_code","supply_item_non_standard.st_classifi_id","supply_item_non_standard.use_status","statistical_classification.name as scf_name")->where("supply_item_non_standard.id",$item_id)->first();
            if($si_arr){
                Cache::add('supply_non_item_'.$item_id,$si_arr,10);
            }
            return $si_arr;
        }

    }

}
